//////////////////////////////////////////////////////////////////////
////                                                              ////
////  CFMemWrite.v                                                ////
////                                                              ////
////  This file is part of the "Pico E12" project                 ////
////  http://www.picocomputing.com                                ////
////                                                              ////
////                                                              ////
//////////////////////////////////////////////////////////////////////
////                                                              ////
//// Copyright (C) 2005, Picocomputing, Inc.                      ////
//// http://www.picocomputing.com/                                ////
////                                                              ////
//// This source file may be used and distributed without         ////
//// restriction provided that this copyright statement is not    ////
//// removed from the file and that any derivative work contains  ////
//// the original copyright notice and the associated disclaimer. ////
////                                                              ////
//// This source file is free software; you can redistribute it   ////
//// and/or modify it under the terms of the GNU Lesser General   ////
//// Public License as published by the Free Software Foundation; ////
//// either version 2.1 of the License, or (at your option) any   ////
//// later version.                                               ////
////                                                              ////
//// This source is distributed in the hope that it will be       ////
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
//// PURPOSE.  See the GNU Lesser General Public License for more ////
//// details.                                                     ////
////                                                              ////
//// You should have received a copy of the GNU Lesser General    ////
//// Public License along with this source; if not, download it   ////
//// from http://www.opencores.org/lgpl.shtml                     ////
////                                                              ////
////////////////////////////////////////////////////////////////////JF

`timescale 10ns / 1ns

module CFMemWrite(CFClock, CardEnabled, OE, WE, IORD, IOWR, REG, 
                  MemWrite, AttribWrite, MemWriteLatch);

input CFClock;                                                       //200 MHz Clock
input CardEnabled;                                                   //Master Card Enable
input OE;                                                            //Memory Read
input WE;                                                            //Memory Write
input IORD;                                                          //I/O Read
input IOWR;                                                          //I/O Write
input REG;                                                           //Attribute Memory Access

output MemWrite;                                                     //Delayed Common Memory Write
output AttribWrite;                                                  //Delayed Attribute Memory Write (AKA: Configuation Memory)
output MemWriteLatch;                                                //Memory Write Hold (Until Card Disconnect)

wire RawMemWrite;
wire RawMemDelayedWrite;
wire RawAttribWrite;
wire RawAttribDelayedWrite;

assign RawAttribWrite = (CardEnabled) && (OE) && (~WE) && (~REG) && (IORD) && (IOWR);
assign RawMemWrite = (CardEnabled) && (OE) && (~WE) && (REG) && (IORD) && (IOWR);

                                                                     //During Memory Writes the Databus can Take up to 35nS to Setup
LongDelay MemWriteLongDelay(CFClock, RawMemWrite, RawMemDelayedWrite);
LongDelay AttribWriteLongDelay(CFClock, RawAttribWrite, RawAttribDelayedWrite);

                                                                     //The following logic ensures that the Memory Write Latch rises slowly and falls instantly
assign MemWriteLatch = ((RawMemDelayedWrite) || (RawAttribDelayedWrite)) && ((RawAttribWrite) || (RawMemWrite));

Delay MemWriteDelay(.Clock(CFClock), .In(RawMemDelayedWrite), .Out(MemWrite));
Delay AttribWriteDelay(.Clock(CFClock), .In(RawAttribDelayedWrite), .Out(AttribWrite)); 

endmodule